import {
  NPCCEnum
} from '@/utils/ccplugin/NPCCEnum'
import {
  ErrorCode
} from '@/utils/ccplugin/NPCCErrorCode'
import {
  NPCCIF
} from '@/utils/ccplugin/NPCCInterface'
import {
  NPCCILY
} from '@/utils/ccplugin/NPCCInterlayer'
import {
  NPCCStruct
} from '@/utils/ccplugin/NPCCStruct'
import {
  NPCCUtils
} from '@/utils/ccplugin/NPCCUtils'
import {
  getSubRes,
  getSubResCameraHandle,
  getSiblingRes,
  onlineDevice
} from './public'
import {
  WebClient
} from './WebClient'
import {WebPreview}from './preview'
export var WebTree = {
  online: 0,
  deviceListSum: 0,
  devs: [],
  showdevs: function () {
    return {
      devs: WebTree.devs,
      online: WebTree.online,
      deviceListSum: WebTree.deviceListSum
    }
  },
  // 构造资源树(支持子域)
  // InsertServerNodes
  ConstructResourceTree: function (hServer, rootDomainName, treeObj, resourceTreeType, callback) {
    // 1、添加树的根节点，既根域
    console.log("*****************")
    console.log(hServer)
    console.log(rootDomainName)
    console.log(treeObj)
    console.log(resourceTreeType)
    WebTree.devs.push({
      hServer: hServer,
      title: rootDomainName,
      children: []
    })


    var html = [];
    html.push('<li><span class="rootName clickNameIcon"></span><span class="clickName" style="">' + rootDomainName + '</span>' +
      '<span class="rootRomainName"></span>');
    html.push('<ul>');
    // 2、添加根域下的所有设备
    WebTree.InsertPUNodes(hServer, html, resourceTreeType, WebTree.devs[WebTree.devs.length - 1].children);

    //获取平台所有子域
    var operator = NPCCIF.RSTW_GetDomainList(NPCCILY.Plug.ccPlugin, WebClient.hServer);
    if (operator.errorCode == ErrorCode.NP_E_OK) {
      var hDomainArray = operator.value; // 子域句柄
      if (hDomainArray && hDomainArray.length > 0) {
        // 3、添加子域
        WebTree.InsertDomainNodes(hServer, hDomainArray, html, resourceTreeType);
      }
    }

    html.push('</ul>');
    html.push('</li>');
    callback(WebTree.showdevs())
    //jq
    // treeObj.html(html.join(''));
    // treeObj.treeview();

    //单击设备 背景颜色
    // $('span.clickName').click(function() {
    //     $('span.clickName').css('background', '');
    //     $(this).css('background', '#5D9BF0');
    // })

    // $('span.clickNameIcon').click(function() {
    //     $('span.clickName').css('background', '');
    //     $(this).next().css('background', '#5D9BF0');
    // })
    // $(treeObj.selector + ' span.rootRomainName').html( '(' + (WebTree.online) + '/' + (WebTree.deviceListSum) + ')');
  },

  // 1、添加树的根节点，既根域
  // 2、添加根域下的所有设备
  InsertPUNodes: function (hRes, treeItem, resourceTreeType, data) {
    var hDomain = '';
    var operator = NPCCIF.COMMON_GetNodeType(NPCCILY.Plug.ccPlugin, hRes);
    if (operator.errorCode == ErrorCode.NP_E_OK) {
      if (operator.value == NPCCEnum.IRstNodeType.NT_Domain) {
        hDomain = hRes;
      }
    }

    if (hDomain == '') {
      operator = NPCCIF.RSTW_GetPUList(NPCCILY.Plug.ccPlugin, hRes); // 获取所有的句柄
      if (operator.errorCode == ErrorCode.NP_E_OK) {
        var hPuArray = operator.value;
        if (!hPuArray) {
          return;
        }
        // 进行在线排序
        var onlines = [];
        var offlines = [];
        hPuArray.map((devHandle, index) => {
          var modelType = NPCCIF.COMMON_GetModelType(NPCCILY.Plug.ccPlugin, devHandle).value;
          if (modelType != NPCCEnum.ModelType.MODEL_TYPE_OSS) {
            var bOnline = NPCCIF.COMMON_IsOnline(NPCCILY.Plug.ccPlugin, devHandle).value;
            var bEnable = NPCCIF.COMMON_IsEnable(NPCCILY.Plug.ccPlugin, devHandle).value;
            // 前端
            if (resourceTreeType == NPCCEnum.resourceTreeTypeDemo.resourceTreeTypeDemo_frond) {
              var operator = NPCCIF.COMMON_GetChildren(NPCCILY.Plug.ccPlugin, devHandle);
              if (operator.errorCode == ErrorCode.NP_E_OK && operator.value) {
                if (operator.value.constructor != Array) {
                  operator.value = [operator.value];
                }
                operator.value.map((resHandle, resIdx) => {
                  var resType = NPCCIF.COMMON_GetResType(NPCCILY.Plug.ccPlugin, resHandle).value;
                  // 存储
                  if (resType == NPCCEnum.ResType.RES_TYPE_STOR) {
                    if (bOnline == 1 && bEnable == 1) {
                      onlines.push(devHandle);
                    } else {
                      offlines.push(devHandle);
                    }
                  }
                })
                // $.each(operator.value, function (resIdx, resHandle) {
                // var resType = NPCCIF.COMMON_GetResType(NPCCILY.Plug.ccPlugin, resHandle).value;
                // // 存储
                // if (resType == NPCCEnum.ResType.RES_TYPE_STOR) {
                //   if (bOnline == 1 && bEnable == 1) {
                //     onlines.push(devHandle);
                //   } else {
                //     offlines.push(devHandle);
                //   }
                // }
                // })
              }
            } else {
              if (bOnline == 1 && bEnable == 1) {
                onlines.push(devHandle);
              } else {
                offlines.push(devHandle);
              }
            }
          }
        })
        // $.each(hPuArray, function(index, devHandle) {
        //     var modelType = NPCCIF.COMMON_GetModelType(NPCCILY.Plug.ccPlugin, devHandle).value;
        //     if (modelType != NPCCEnum.ModelType.MODEL_TYPE_OSS) {
        //         var bOnline = NPCCIF.COMMON_IsOnline(NPCCILY.Plug.ccPlugin, devHandle).value;
        //         var bEnable = NPCCIF.COMMON_IsEnable(NPCCILY.Plug.ccPlugin, devHandle).value;
        //         // 前端
        //         if (resourceTreeType == NPCCEnum.resourceTreeTypeDemo.resourceTreeTypeDemo_frond) {
        //             var operator = NPCCIF.COMMON_GetChildren(NPCCILY.Plug.ccPlugin, devHandle);
        //             if (operator.errorCode == ErrorCode.NP_E_OK && operator.value) {
        //                 if (operator.value.constructor != Array) {
        //                     operator.value = [operator.value];
        //                 }

        //                 $.each(operator.value, function(resIdx, resHandle) {
        //                     var resType = NPCCIF.COMMON_GetResType(NPCCILY.Plug.ccPlugin, resHandle).value;
        //                     // 存储
        //                     if (resType == NPCCEnum.ResType.RES_TYPE_STOR) {
        //                         if (bOnline == 1 && bEnable == 1) {
        //                             onlines.push(devHandle);
        //                         }
        //                         else {
        //                             offlines.push(devHandle);
        //                         }
        //                     }
        //                 })
        //             }
        //         }
        //         else {
        //             if (bOnline == 1 && bEnable == 1) {
        //                 onlines.push(devHandle);
        //             }
        //             else {
        //                 offlines.push(devHandle);
        //             }
        //         }
        //     }

        // });

        var puResHandleListTemp = [];
        puResHandleListTemp = puResHandleListTemp.concat(onlines);
        puResHandleListTemp = puResHandleListTemp.concat(offlines);

        WebTree.online = onlines.length;
        WebTree.deviceListSum = puResHandleListTemp.length;
        for (var i = 0; i < puResHandleListTemp.length; i++) {
          WebTree.InsertPUNode(puResHandleListTemp[i], treeItem, resourceTreeType, data);
        }
      }
    } else {
      // 该域所有设备
      operator = NPCCIF.DOMAIN_GetPUList(NPCCILY.Plug.ccPlugin, hDomain);
      if (operator.errorCode == ErrorCode.NP_E_OK) {
        var hPuArray = operator.value;
        // 进行在线排序
        var onlines = [];
        var offlines = [];
        hPuArray.map((devHandle, index) => {
          var bOnline = NPCCIF.COMMON_IsOnline(NPCCILY.Plug.ccPlugin, devHandle).value;
          var bEnable = NPCCIF.COMMON_IsEnable(NPCCILY.Plug.ccPlugin, devHandle).value;
          // 前端
          if (resourceTreeType == NPCCEnum.resourceTreeTypeDemo.resourceTreeTypeDemo_frond) {
            var operator = NPCCIF.COMMON_GetChildren(NPCCILY.Plug.ccPlugin, devHandle);
            if (operator.errorCode == ErrorCode.NP_E_OK && operator.value) {
              if (operator.value.constructor != Array) {
                operator.value = [operator.value];
              }
              operator.value.map((resHandle, resIdx) => {
                var resType = NPCCIF.COMMON_GetResType(NPCCILY.Plug.ccPlugin, resHandle).value;
                // 存储
                if (resType == NPCCEnum.ResType.RES_TYPE_STOR) {
                  if (bOnline == 1 && bEnable == 1) {
                    onlines.push(devHandle);
                  } else {
                    offlines.push(devHandle);
                  }
                }
              })

            }
          } else {
            if (bOnline == 1 && bEnable == 1) {
              onlines.push(devHandle);
            } else {
              offlines.push(devHandle);
            }
          }
        })
        // $.each(hPuArray, function(index, devHandle) {
        //     var bOnline = NPCCIF.COMMON_IsOnline(NPCCILY.Plug.ccPlugin, devHandle).value;
        //     var bEnable = NPCCIF.COMMON_IsEnable(NPCCILY.Plug.ccPlugin, devHandle).value;
        //     // 前端
        //     if (resourceTreeType == NPCCEnum.resourceTreeTypeDemo.resourceTreeTypeDemo_frond) {
        //         var operator = NPCCIF.COMMON_GetChildren(NPCCILY.Plug.ccPlugin, devHandle);
        //         if (operator.errorCode == ErrorCode.NP_E_OK && operator.value) {
        //             if (operator.value.constructor != Array) {
        //                 operator.value = [operator.value];
        //             }

        //             $.each(operator.value, function(resIdx, resHandle) {
        //                 var resType = NPCCIF.COMMON_GetResType(NPCCILY.Plug.ccPlugin, resHandle).value;
        //                 // 存储
        //                 if (resType == NPCCEnum.ResType.RES_TYPE_STOR) {
        //                     if (bOnline == 1 && bEnable == 1) {
        //                         onlines.push(devHandle);
        //                     }
        //                     else {
        //                         offlines.push(devHandle);
        //                     }
        //                 }
        //             })
        //         }
        //     }
        //     else {
        //         if (bOnline == 1 && bEnable == 1) {
        //             onlines.push(devHandle);
        //         }
        //         else {
        //             offlines.push(devHandle);
        //         }
        //     }
        // });

        var puResHandleListTemp = [];
        puResHandleListTemp = puResHandleListTemp.concat(onlines);
        puResHandleListTemp = puResHandleListTemp.concat(offlines);
        for (var i = 0; i < puResHandleListTemp.length; i++) {
          WebTree.InsertPUNode(puResHandleListTemp[i], treeItem, resourceTreeType, data);
        }
      }
    }
  },

  // 3、添加子域
  InsertDomainNodes: function (pServer, hDomainArray, treeItem, resourceTreeType) {
    if (hDomainArray && hDomainArray.length > 0) {
      console.log('_____________________')
      console.log(hDomainArray)
      for (var i = 0; i < hDomainArray.length; i++) {
        var hDomain = hDomainArray[i];
        var operator = NPCCIF.DOMAIN_GetName(NPCCILY.Plug.ccPlugin, hDomain);
        // treeItem.push('<li><span class="rootName clickNameIcon"></span><span class="clickName" style="">' + operator.value + '</span>' +
        //     '<span class=""></span>');
        // treeItem.push('<ul>');

        // 添加子域下的设备
        WebTree.InsertPUNodes(hDomain, treeItem, resourceTreeType);
        // treeItem.push('</ul>');
        // treeItem.push('</li>');
        // 插入子域下的子域节点
        var operator = NPCCIF.DOMAIN_GetSubDomainList(NPCCILY.Plug.ccPlugin, hDomain);
        if (operator.errorCode == ErrorCode.NP_E_OK) {
          WebTree.InsertDomainNodes(pServer, operator.value, treeItem, resourceTreeType);
        }
      }
    }
  },

  InsertPUNode: function (devHandle, treeItem, resourceTreeType, data) {
    // 获取pPU下的子资源，然后遍历，并调用下面的方法去向树添加子资源
    var modelType = NPCCIF.COMMON_GetModelType(NPCCILY.Plug.ccPlugin, devHandle).value;
    if (modelType != NPCCEnum.ModelType.MODEL_TYPE_OSS) {
      var bOnline = NPCCIF.COMMON_IsOnline(NPCCILY.Plug.ccPlugin, devHandle).value;
      var bEnable = NPCCIF.COMMON_IsEnable(NPCCILY.Plug.ccPlugin, devHandle).value;
      var puid = NPCCIF.COMMON_GetPUID(NPCCILY.Plug.ccPlugin, devHandle).value;
      var name = NPCCUtils.UTF8toUnicode(NPCCUtils.UnicodetoUTF8(NPCCIF.COMMON_GetName(NPCCILY.Plug.ccPlugin, devHandle).value));
      if (bOnline == 1 && bEnable == 1) {
        if (modelType == NPCCEnum.ModelType.MODEL_TYPE_WENC) {
          console.log('无线编码器' + name)
          // 无线编码器
          // treeItem.push('<li><span class="equipNameOn clickNameIcon"></span><span class="clickName" style="padding: 3px;">' + name + '</span>');
        } else if (modelType == NPCCEnum.ModelType.MODEL_TYPE_ENC) {
          // 有线编码器
          console.log("有线编码器" + name)
          data.push({

            title: name,
            children: []
          })
          data = data[data.length-1].children
          // treeItem.push('<li><span class="equipNameOnWired clickNameIcon"></span><span class="clickName" style="padding: 3px;">' + name + '</span>');
        }
      } else {
        if (modelType == NPCCEnum.ModelType.MODEL_TYPE_WENC) {
          // 无线编码器
          console.log('无线编码器' + name)
          // treeItem.push('<li><span class="equipNameOff clickNameIcon"></span><span class="clickName" style="padding: 3px;">' + name + '</span>');
        } else if (modelType == NPCCEnum.ModelType.MODEL_TYPE_ENC) {
          // 有线编码器
          console.log('有线编码器' + name)
          // treeItem.push('<li><span class="equipNameOffWired clickNameIcon"></span><span class="clickName" style="padding: 3px;">' + name + '</span>');
        }
      }

      // treeItem.push('<ul>');
      WebTree.InsertResNodes(devHandle, treeItem, resourceTreeType, data);
      // treeItem.push('</li>');
    }
  },

  InsertResNodes: function (devHandle, treeItem, resourceTreeType, data) {
    // if ($('.sysManageLi').hasClass('active')) {
    if (false) {
      // 系统管理页面
    } else {
      // 添加设备子资源  //摄像头句柄
      console.log('添加设备子资源')
      var resHandle = getSubResCameraHandle(devHandle, NPCCEnum.ResType.RES_TYPE_CAM);
      if (resHandle && resHandle.length) {
        var bOnline = NPCCIF.COMMON_IsOnline(NPCCILY.Plug.ccPlugin, devHandle).value;
        var bEnable = NPCCIF.COMMON_IsEnable(NPCCILY.Plug.ccPlugin, devHandle).value;
        var puid = NPCCIF.COMMON_GetPUID(NPCCILY.Plug.ccPlugin, devHandle).value;
        for (var i = 0; i < resHandle.length; i++) {
          var name = NPCCUtils.UTF8toUnicode(NPCCUtils.UnicodetoUTF8(NPCCIF.COMMON_GetName(NPCCILY.Plug.ccPlugin, resHandle[i]).value));
          if (bOnline == 1 && bEnable == 1) {
            console.log(resHandle[i])
            console.log(name)
            data.push({
              hServer: resHandle[i],
              title: name,
              children: []
            })
            // treeItem.push('<li ondblclick="WebTree.dblClickEvent(\'' + resHandle[i] + '\')">' +
            //     '<span class="cameraNameOn clickNameIcon"></span><span class="clickName" style="padding: 3px;">' + name + '</span></li>');
          } else {
            if (resourceTreeType == NPCCEnum.resourceTreeTypeDemo.resourceTreeTypeDemo_frond) { // 前端
              console.log(name)
              // treeItem.push('<li><span class="cameraNameOff clickNameIcon"></span><span class="clickName" style="padding: 3px;">' + name + '</span></li>');
            } else { // 平台
              data.push({
                hServer: resHandle[i],
                title: name,
                children: []
              })
              console.log(resHandle[i])
              console.log(name)
              // treeItem.push('<li ondblclick="WebTree.dblClickEvent(\'' + resHandle[i] + '\')">' +
              //     '<span class="cameraNameOff clickNameIcon"></span><span class="clickName" style="padding: 3px;">' + name + '</span></li>');
            }
          }
        }

      }

    }

    // treeItem.push('</ul>');
  },

  // 双击事件
  dblClickEvent: function(resHandle) {
      // if ($('.previewLi').hasClass('active')) {
      //     // 实时指挥页面,点击播放视频
          WebPreview.ClickCameraRes(resHandle);
      // }
      // else if ($('.queryLi').hasClass('active')) {
      //     var platformOrLocalSelectText = $("#platformOrLocalSel").find("option:selected").text();
      //     var viewOrPhotoSelectText = $("#viewOrPhotoSel").find("option:selected").text();
      //     if (platformOrLocalSelectText == '平台') {
      //         if (viewOrPhotoSelectText == '录像') {
      //             // 查询回放页面， 点击获取平台 录像 文件（先获取日期信息）
      //             WebQuery.platformVideoInfoQuery(resHandle);
      //         }
      //         else {
      //             // 查询回放页面， 点击获取平台 图片 文件（先获取日期信息）
      //             WebQuery.platformInfoPhotoQuery(resHandle);
      //         }

      //     }

      // }
      // else if ($('.playbackLi').hasClass('active')) {
      //     // 实时回放页面，点击回放视频
      //     WebPreview.ClickPlayVideoPlayback(resHandle);
      // }
  }
};